package com.zxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



import com.zxh.dao.EducationDao;
import com.zxh.dao.ResumeDao;
import com.zxh.entity.Education;
import com.zxh.entity.ResumeBasicInfo;
@WebServlet("/edu.do")
public class EducationServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			HttpSession session =req.getSession();
			String type = req.getParameter("type");//表示保存的类型
			PrintWriter out=resp.getWriter();
			int applicant_id=0;
			if(session.getAttribute("applicant_id")!=null){
				applicant_id = (Integer) session.getAttribute("applicant_id");
			}
			Education education =getRequestData(req);
			EducationDao eduDao = new EducationDao(); 
			JSONObject json=new JSONObject();
			if("save".equals(type)){
				eduDao.save(education);//执行添加教育经历，执行需要返回列表（这个时候要执行查询操作）
				//教育经历
				List<Education>edulist=eduDao.getEducationList(applicant_id);//这个集合需要返到页面
				try {
					JSONArray jsonarray=JSONArray.fromObject(edulist);
					
					/*json.put("edulist",jsonarray );*/
					json.put("code", 0);
					json.put("message", "添加成功");
					System.out.println(jsonarray.toString());
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "添加失败");
					e.printStackTrace();
				}
				out.println(json.toString());

			}else if("update".equals(type)){
				String  education_id = req.getParameter("education_id");
				education.setEducation_id(Integer.parseInt(education_id));
				try {
					eduDao.update(education);
					json.put("code", 0);
					json.put("message", "修改成功");
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "修改失败");
					e.printStackTrace();
				}
				out.println(json.toString());
			}else if("delete".equals(type)){
				String  education_id = req.getParameter("education_id");
				eduDao.delete(Integer.valueOf(education_id));
				json.put("code", 0);
				json.put("message", "删除成功");
				try {
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "删除失败");
					e.printStackTrace();
				}
				out.println(json.toString());
			
			}
			
			
		
			//获取简历基本信息
			/*int basicInfo_id = 0;
			if(session.getAttribute("basicinfoId")!=null){
				basicInfo_id=(Integer) session.getAttribute("basicinfoId");
			}
				ResumeDao resumedao  = new ResumeDao();
				ResumeBasicInfo resumeInfo=resumedao.queryResuemBasicInfo(basicInfo_id);
				req.setAttribute("resuemInfo", resumeInfo);
				req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp); 
				
				List<Education>edulist=eduDao.getEducationList(applicant_id);//这个集合需要返到页面
				JSONObject json=new JSONObject();
				try {
					JSONArray jsonarray=JSONArray.fromObject(edulist);
					
					json.put("edulist",jsonarray );
					json.put("code", 0);
					json.put("message", "添加成功");
					System.out.println(jsonarray.toString());
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "添加失败");
					e.printStackTrace();
				}
				out.println(json.toString());*/
				
		
		
		
	}
	private Education getRequestData(HttpServletRequest req){
		 HttpSession session = req.getSession();
		 String school = req.getParameter("school");
		 String beginTime = req.getParameter("beginTime");
		 String schooling = req.getParameter("schooling") ;
		 String profession = req.getParameter("profession");
		/* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");*/
		 int applicant_id=0;
		 if(session.getAttribute("applicant_id")!=null){
			 applicant_id = (Integer) session.getAttribute("applicant_id");
		 }
		 /*Date beginTimeDate = null;
		 try {
			beginTimeDate =sdf.parse(beginTime);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		 Education education = new Education(applicant_id,school,beginTime,schooling,profession);
		 return education;
	}
	
}
